package cn.tedu.ivosback.geofence.service.impl;

import cn.tedu.ivosback.base.exception.ServiceException;
import cn.tedu.ivosback.base.response.StatusCode;
import cn.tedu.ivosback.geofence.mapper.GeofenceMapper;
import cn.tedu.ivosback.geofence.pojo.dto.GeofenceParam;
import cn.tedu.ivosback.geofence.pojo.dto.GeofenceQuery;
import cn.tedu.ivosback.geofence.pojo.entity.Geofence;
import cn.tedu.ivosback.geofence.pojo.vo.GeofenceVO;
import cn.tedu.ivosback.geofence.service.GeofenceService;
import cn.tedu.ivosback.vehicle.mapper.VehicleMapper;
import cn.tedu.ivosback.vehicle.pojo.dto.VehicleQuery;
import cn.tedu.ivosback.vehicle.pojo.vo.VehicleVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Slf4j
@Service
public class GeofenceServiceImpl implements GeofenceService {
    @Autowired
    GeofenceMapper geofenceMapper;
    @Autowired
    VehicleMapper vehicleMapper;

    @Override
    public List<GeofenceVO> selectGeofence(GeofenceQuery geofenceQuery) {
        log.debug("查询电子围栏列表:geofenceQuery={}", geofenceQuery);
        List<GeofenceVO> list = geofenceMapper.selectGeofence(geofenceQuery);
        if (list.size() != 0 && list != null) {
            for (int i = 0; i < list.size(); i++) {
                GeofenceVO geofenceVO = list.get(i);
                VehicleQuery vehicleQuery = new VehicleQuery();
                vehicleQuery.setGeofenceId(geofenceVO.getId());
                List<VehicleVO> listVehicle = vehicleMapper.selectVehicle(vehicleQuery);
                geofenceVO.setVehicleList(listVehicle);
                geofenceVO.setTotalNum(listVehicle.size());
                int availableNum = 0;
                for (int j = 0; j < listVehicle.size(); j++) {
                    if (listVehicle.get(j).getStatus().equals("1")) {
                        availableNum++;
                    }
                }
                geofenceVO.setAvailableNum(availableNum);
            }
        }
        return list;
    }

    @Override
    public void saveGeofence(GeofenceParam geofenceParam) {
        log.debug("保存电子围栏:geofenceParam={}", geofenceParam);
        Geofence geofence = new Geofence();
        BeanUtils.copyProperties(geofenceParam, geofence);
        if (geofenceParam.getId() != null) {
            geofence.setUpdateTime(new Date());
            geofenceMapper.updateGeofence(geofence);
        } else {
            geofence.setStatus("1");
            geofence.setCreateTime(new Date());
            geofenceMapper.insertGeofence(geofence);
        }
    }

    @Override
    public void deleteGeofence(Long id) {
        log.debug("删除电子围栏:id={}", id);
        VehicleQuery vehicleQuery = new VehicleQuery();
        vehicleQuery.setGeofenceId(id);
        List<VehicleVO> list = vehicleMapper.selectVehicle(vehicleQuery);
        if (list.size() > 0 && list != null) {
            throw new ServiceException(StatusCode.VEHICLE_EXISTS);
        } else {
            geofenceMapper.deleteGeofence(id);
        }
    }
}
